<template>
    <div class="list" ref="wrapper">
        <div class="content">
            <div class="area">
                <div class="title">当前城市</div>
                <div class="button-list">
                    <div class="button-wrapper">
                        <div class="button">{{this.$store.state.city}}</div>
                    </div>
                </div>
            </div>
            <div class="area">
                <div class="title">热门城市</div>
                <div class="button-list">
                    <div
                        class="button-wrapper"
                        v-for="item in hot"
                        :key="item.id"
                        @click="handleCityClick(item.name)"
                    >
                        <div class="button">{{ item.name }}</div>
                    </div>
                </div>
            </div>
            <div class="area" v-for="(item,key) in cities" :key="key" :ref="key">
                <div class="title">{{key}}</div>
                <div class="item-list">
                    <div class="item" v-for="innerItem in item" :key="innerItem.id" @click="handleCityClick(innerItem.name)">{{innerItem.name}}</div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import BScroll from "better-scroll"

export default {
    name: "CityList",
    props: {
        cities: Object,
        hot: Array,
        letter:String
    },
    watch: {
        letter(){
            if (this.letter) {
                const element = this.$refs[this.letter][0]
                this.scroll.scrollToElement(element)
            }
        }
    },
    methods:{
        handleCityClick(city){
            this.$store.dispatch('changeCity',city)
            this.$router.push('/')
        }
    },
    mounted() {
        this.scroll = new BScroll(this.$refs.wrapper)
    }
}
</script>

<style scoped>
.list {
    overflow: hidden;
    position: absolute;
    top: 1.68rem;
    left: 0;
    right: 0;
    bottom: 0;
}
.title {
    line-height: 0.54rem;
    background: #eee;
    padding-left: 0.2rem;
    color: #666;
    font-size: 0.26rem;
    border-bottom: 1px solid rgb(218, 217, 217);
}
.button-list {
    height: auto;
    overflow: hidden;
    padding: 0.1rem 0.6rem 0.1rem 0.1rem;
}
.button-wrapper {
    float: left;
    width: 33.33%;
}
.button {
    margin: 0.1rem;
    padding: 0.1rem;
    text-align: center;
    border: 0.02rem solid #ccc;
    border-radius: 0.06rem;
}
.item {
    line-height: 0.76rem;
    color: #666;
    padding-left: 0.2rem;
    border-bottom: 0.02rem solid #ccc;
}
</style>